package thread;

import javax.swing.*;
import java.awt.*;

public class ParticleController extends Thread {
    private final int INI_X = 100, INI_Y = 500;
    private int size = 1;
    MyColor color = new MyColor(50, 10, 20);
    private JFrame jf;

    static class MyColor {
        int r, g, b;

        public MyColor(int r, int g, int b) {
            this.r = r;
            this.g = g;
            this.b = b;
        }

        public void brighter() {
            this.r += 4;
            this.g += 4;
            this.b += 4;
        }
    }

    public ParticleController(JFrame jf) {
        this.jf = jf;
    }

    @Override
    public void run() {
        super.run();
        Vec pos = new Vec(INI_X, INI_Y);            // 位置
        Vec velocity = new Vec(100, -100);    // 速度
        Vec acceleration = new Vec(2, 20);   // 加速度
        int dSize = 2;

        double dt = 0.1d;
        Graphics g = this.jf.getGraphics();
        for (int i = 0; i < 40; ++i) {
            pos = pos.add(velocity.multi(dt));  // 下一个位置
            velocity = velocity.add(acceleration.multi(dt));   // 下一个时间
            g.setColor(new Color(color.r, color.g, color.b));
            g.fillOval((int) pos.x, (int) pos.y, size, size);
            this.color.brighter();
            this.size += dSize;
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
